# Copyright (c) 2012-2013 MIT License by 6.172 Staff
CC := clang
CFLAGS := -g -Wall -std=gnu99 -gdwarf-3 -always-inline
LDFLAGS := -lrt -lm 
# You may add new files to the list of COMMON_SRC below
COMMON_SRC := tests.c util.c isort.c sort_a.c sort_c.c sort_i.c sort_p.c sort_m.c sort_f.c
COMMON_HEADERS := fasttime.h util.h tests.h
TARGET := sort

ifeq ($(DEBUG),1)
CFLAGS := -DDEBUG -O0 $(CFLAGS)
else
CFLAGS := -O3 -DNDEBUG $(CFLAGS)
endif
CFLAGS := $(CFLAGS)

all: $(TARGET)

sort: main.c $(COMMON_SRC) $(COMMON_HEADERS) Makefile
	$(CC) main.c $(COMMON_SRC) $(CFLAGS) $(LDFLAGS) -o sort

lint:
	python clint.py *.[ch]
# run sort on input sizes around a power of 2
SIZE=131072
run: sort
	@echo "\n\nSorting 2^17 - 1 elements"
	./sort 524287 1
	@echo "\n\nSorting 2^17 elements"
	./sort 524288 1
	@echo "\n\nSorting 2^17 + 1 elements"
	./sort 524289 1

small:SIZE=32
small:sort
	@echo "\n\nSorting 2^5 printed"
	./sort -p ${SIZE} 1

# echo $[1<<22]
huge:SIZE=262144
huge:sort
	./sort ${SIZE} 2

stat:sort
	valgrind --tool=cachegrind --branch-sim=yes ./sort ${SIZE} 1

aws_stat:sort
	awsrun valgrind --tool=cachegrind --branch-sim=yes ./sort ${SIZE} 1

# kcachegrind to display trace output
# callgrind:SIZE=131072
# callgrind:sort
#	valgrind --tool=callgrind --vgdb=no  --trace-children=no -v ./sort ${SIZE} 1
#	@echo
#	@echo Now start kcachegrind\&

# cq_callgrind:SIZE=131072
# cq_callgrind:sort
#	cqrun valgrind --tool=callgrind --vgdb=no  --trace-children=no -v ./sort ${SIZE} 1
#	@echo
#	@echo Now start kcachegrind\&

memcheck:sort
	valgrind --leak-check=full  ./sort 1000 1

pre-submit:
	make DEBUG=1 clean small
	make memcheck
	make clean small
	make memcheck

# remove all targets as well as output generated by PNQ
clean::
	rm -f ./sort *.std* *.gcov *.gcda *.gcno default.profraw
